import * as THREE from 'three';
const mouse = new THREE.Vector2();
const raycaster = new THREE.Raycaster();

class Selector {
    constructor(scene) {

        this.scene = scene;
    }
    getIntersects(raycaster) {

        const objects = [];

        this.scene.traverseVisible(function (child) {
            
             objects.push(child);

        });


        return raycaster.intersectObjects(objects, false);

    }
    getPointerIntersects(point, camera) {

        mouse.set((point.x * 2) - 1, - (point.y * 2) + 1);

        raycaster.setFromCamera(mouse, camera);

        return this.getIntersects(raycaster);

    }

    getPoint(point, camera) {
        mouse.set((point.x * 2) - 1, - (point.y * 2) + 1);

        raycaster.setFromCamera(mouse, camera);
        // 射线交互 当前如果没有相交场景物体 就 拿点位

        const intersection = new THREE.Vector3();
        const plane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0); // 水平面
        raycaster.ray.intersectPlane(plane, intersection);
        return { point: intersection, object: null };

    }
}

export { Selector }